PWM - Programming Arduino using Matlab 4

 In this tutorial you can learn how to use the PWM signal generation function in Matlab to program Arduino to control brightness of a LED and control a DC motor. The Matlab function for Arduino that generates PWM signal with specified duty cycle is writePWMDutyCycle(). We will write a Matlab program using a for and while loop to slowly increase and gradually decrease PWM signal strength. This signal will be fed into Arduino UNO which in turn will drive a LED and a DC motor.

PWM controlled DC motor using Matlab Arduino

This is 4th part of the tutorial series Programming Arduino using Matlab. Previous tutorial that you might want to read are:

- LED Blinking

Push Button Controlled LED

Analog Read and Processing



Controlling LED brightness using Matlab Arduino

The following is the Matlab program that generates PWM signal of varying duty cycle on the digital pin 10 of Arduino.

clear;

clc

disp('use CTRL+C to exit');


ledPin = 'D10';


ard = arduino();


stepsize = 0.1;


while 1

for i=0:10

    writePWMDutyCycle(ard, ledPin, i*stepsize);

    pause(0.1);

end


for i=0:10

    writePWMDutyCycle(ard, ledPin, 1-i*stepsize);

    pause(0.1);

end

pause(0.1);

end


The PWM signal is generated from Arduino using Matlab function writePWMDutyCycle() which accepts three parameters. First is the arduino object(ard in above program code) which you have to first create in Matlab program using the arduino() function. Then you need to specify which Arduino digital pin where the PWM signal should appear. This is the digital pwm pin 10 named alias as ledPin in the above program code. Then lastly you should specify the duty cycle which ranges from 0 to 1. In the above program we have used for loop to loop through varying duty cycle. In the above program we first increase gradually the PWM duty cycle which will turn the led brighter and the decrease using another for loop to decrease led brightness. This increase decrease of brightness keeps cycling because of the while loop.

PWM controlled LED using Matlab Arduino

Video Demonstration

Watch the following video how the PWM control of LED brightness works with Matlab/Arduino programming.



PWM controlled DC motor using Arduino/Matlab

The Matlab program code for Arduino for controlling DC motor using PWM signal is below.

clear;

clc

disp('use CTRL+C to exit');


motPin = 'D10';


ard = arduino();


stepsize = 0.1;


while 1

for i=0:10

    writePWMDutyCycle(ard, motPin, i*stepsize);

    pause(0.1);

end


pause(0.1);


for i=0:10

    writePWMDutyCycle(ard, motPin, 1-i*stepsize);

    pause(0.1);

end

pause(0.1);

end


The program code is similar to program code for controlling brightness of a LED. Again we have used the writePWMDutyCycle() function with 3 parameters as already explained above.


The hardware connection for controlling DC motor is different unlike the LED. For DC motor, we need to add protection diode 1N4007 between the motor terminals with proper polarity. Also we have added a 2N2222 transistor. The PWM signal from Arduino goes into the base of the transistor. The motor along with the protection diode is connected at the collector. The emitter is grounded. We also used separate power supply for the motor. A small DC motor was used and so power supply of 5V was sufficient. 

Video Demonstration

The following video demonstrates PWM control of DC using Matlab programming of Arduino.


Post a Comment

Previous Post Next Post